
(function($){
	$.fn.workflow = function(opts){
		var options = $.extend($.fn.workflow.defaults, opts?opts:{});
		
		var $table = this;
		var $template = this.find(options.bindTemplate);
		
		function validate(){
			var $form = $(options.form);
			var $input = $form.find("tr:not(#workflow-template)").find("input.required");
			var $select = $form.not("#workflow-template").find("select.required");
			
			var isValid = true;
			$.each($input, function(i, n){
				var $n = $(n);
				if(!$n.val()){
					isValid = false;
					$n.addClass("error");
					if($n.next().length<1){						
						$n.after("<span class='help-inline error'>请输入步骤名称</span>");
					}
				}else{
					$n.removeClass("error");
					$n.next(".error").remove();
				}
			});
			$.each($select, function(i, n){
				var $n = $(n);
				if(!$n.find("option:selected").val()){
					isValid = false;
					$n.addClass("error");
					if($n.next().length<1){
						$n.after("<span class='help-inline error'>请选择审批角色</span>");
					}
				}else{
					$n.removeClass("error");
					$n.next(".error").remove();
				}
			});
			return isValid;
		}
		
		
		function createId(){
			var lastId = $table.find("tr:last input[name=id]").val();
			if(!lastId)
				lastId = "0";
			lastId = parseInt(lastId)+1;
			return lastId;
		}
		
		function add(){
			var _id = createId();
			var _template = $template.clone();
			$table.append(_template);
			
			_template.removeAttr("id");
			_template.removeClass("hide");
			
			_template.find("input[name=id]").val(_id);
			_template.find("input[name=id]").after(_id);
		}
		
		function remove(){
			$(this).parent().parent().remove();
		}
		
		function save(){
			var $tr = $table.find("tr").slice(2);
			
			if(!$tr || $tr.length<1){
				options.errorMessage("未添加步骤");
				return;
			}
			
			if(!validate()){
				options.errorMessage("存在未填写的项");
				return;
			}
			
			var model = [];
			$.each($tr, function(i,n){
				var _tr = $(n);
				var doc = _tr.find("input[name=documentation]").val();
				model.push({
					id: "userTask_"+_tr.find("input[name=id]").val(),
					name: _tr.find("input[name=name]").val(),
					candidateGroups: _tr.find("select[name=candidateGroups]").val(),
					documentation: doc?doc:""
				});
			});
			
			var params = options.params();
			params.model = JSON.stringify(model);
			
			$.post(options.url, params, function(msg){
				options.resultMessage(msg);
			}, "json");
		}
		
		this.find(options.bindAdd).live("click", add);
		this.find(options.bindRemove).live("click", remove);
		$(options.bindSave).bind("click", save);
		
	};
	
	
	$.fn.workflow.defaults = {
		form: "#workflowDefintion-form",
		bindAdd: ".workflow-add",
		bindRemove: ".workflow-remove",
		bindTemplate: "#workflow-template",
		bindSave: ".workflow-save",
		url: "#",
		successUrl: "#",
		params: function(){
			return {};
		},
		errorMessage: function(msg){
			$(".alert-error").html(msg);
			$(".alert-error").show();
		},
		resultMessage: function(msg){
			if(msg.status == "error"){
				var error = $(".alert-error");
				error.html(msg);
				error.show();
	            App.scrollTo(error, -200);
			}else if(msg.status == "success"){
				if($.isFunction(this.successUrl)){
					window.location.href = this.successUrl.call();
				}else{
					window.location.href = this.successUrl;
				}
			}
		}
	};
})(jQuery);